package com.mybatisx.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mybatisx.mybatisplus.entity.User;
import com.mybatisx.mybatisplus.mapper.UserMapper;
import com.mybatisx.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyh
 * @since 2021-06-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

         @Autowired
         private UserMapper userMapper;
    //根据id查询用户
    @Override
    @Cacheable(value = "users" , key="#id")
    public User findUserById(String id){
        return  userMapper.selectById(id);
    }

    //添加用户
    @Override
    @CachePut(value = "users",key = "#user.id")
    public int saveUser(User user) {
        return  userMapper.insert(user);
    }

    //根据ID删除用户
    @Override
    @CacheEvict(value = "users",key = "#id")
    public int delete(String id) {
       return userMapper.deleteById(id);
    }
}
